package de.gwtprime.shared;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.rpc.IsSerializable;

import de.gwtprime.shared.TradingPosition.OpenStratType;
import de.gwtprime.shared.TradingPosition.TradingPositionType;


/**
 * Result of a specific trade strategy. Contains the complete position history of this strategy run
 * @author jan
 *
 */
public class TradeResult implements  Serializable, IsSerializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1430089216600941904L;
	
	/**
	 * Position history
	 */
	private List<TradingPosition> positions = new ArrayList<TradingPosition>();
	
	public TradeResult(){}
	
	public TradeResult(List<TradingPosition> positions){
		this.positions = positions;
	}

	public List<TradingPosition> getPositions() {
		return positions;
	}

	public void setPositions(List<TradingPosition> positions) {
		this.positions = positions;
	}
	
	
	/**
	 * Get percentage of profitable trades
	 * @return
	 */
	public double getWinTrades(){
	
		int winTrades = 0;
		for(TradingPosition p : positions){
			if(p.getProfit()>0){
				winTrades ++;
			}
		}
		
		return (double)winTrades/positions.size();
		
	}
	
	/**
	 * Get percentage of profitable trades by type (short long)
	 * @return
	 */
	public double getWinTrades(TradingPositionType type){
	
		int numberOfTradesOfThisType = 0;
		int winTrades = 0;
		for(TradingPosition p : positions){
			if(p.getTradingType() == type ){
				
				numberOfTradesOfThisType ++;
				if(p.getProfit()>0)
					winTrades ++;
			}
		}
		
		return (double)winTrades/numberOfTradesOfThisType;
		
	}
	
	
	/**
	 * Get percentage of profitable trades by open strat
	 * @return
	 */
	public double getWinTrades(OpenStratType type){
	
		int numberOfTradesOfThisType = 0;
		int winTrades = 0;
		for(TradingPosition p : positions){
			if(p.getOpenStratType() == type ){
				
				numberOfTradesOfThisType ++;
				if(p.getProfit()>0)
					winTrades ++;
			}
		}
		
		return (double)winTrades/numberOfTradesOfThisType;
		
	}
	
	/**
	 * Get average profit per trade per open strat.
	 * @return
	 */
	public double getAvgProfitPerTradePerStrat(OpenStratType type){
	
		int numberOfTradesOfThisType = 0;
		int sumProfit = 0;
		for(TradingPosition p : positions){
			if(p.getOpenStratType() == type ){
				
				numberOfTradesOfThisType ++;
				
					sumProfit += p.getProfit();
			}
		}
		
		return (double)sumProfit/numberOfTradesOfThisType;
		
	}
	
	/**
	 * Get complete profit of all positions.
	 */
	public double getCompleteProfit(){
		
		double completeProfit = 0;
		for(TradingPosition position : getPositions()){
			completeProfit += position.getProfit();
		}
		
		return completeProfit;
	}
	
}
